package com.example.project.dao.impl;

import com.example.project.dao.IAttendanceDao;
import com.example.project.entity.Attendance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class AttendanceDaoImpl implements IAttendanceDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    //实现添加方法
    @Override
    public int add(Attendance attendance) {
        return jdbcTemplate.update("insert into attendance(AttendanceId,ClockinTime,ClockoutTime,IP,EmployeeId)"+ "values (?,?,?,?,?)",
                attendance.getAttendanceId(),attendance.getClockinTime(),attendance.getClockoutTime(),attendance.getIP(),attendance.getEmployeeId());
    }

    //实现查询所有数据的方法
    @Override
    public List<Attendance> getAllAttendance(Attendance attendance) {
        return jdbcTemplate.query("select AttendanceId,ClockinTime,ClockoutTime,IP,EmployeeId from Attendance",new Object[]{},
                new BeanPropertyRowMapper<>(Attendance.class));
    }

    //实现删除打卡数据的方法
    @Override
    public int deleteAttendance(Attendance attendance) {
        return jdbcTemplate.update("delete from Attendance where AttendanceId=?",
                attendance.getAttendanceId());
    }

    //实现打卡信息的更新
    @Override
    public int updateAttendance(Attendance attendance) {
        return jdbcTemplate.update("update Attendance set"+" ClockinTime=?,ClockoutTime=?,IP=?,EmployeeId=? where AttendanceId=?",
                attendance.getClockinTime(),
                attendance.getClockoutTime(),
                attendance.getIP(),
                attendance.getEmployeeId(),
                attendance.getAttendanceId());
    }

    //实现单挑查询打卡数据方法
    @Override
    public Attendance getAttendance(Attendance attendance) {
        List<Attendance> result =jdbcTemplate.query("select"+"`AttendanceId`,`ClockinTime`,`ClockoutTime`,`IP`,`EmployeeId`"+"from Attendance where AttendanceId='"+attendance.getAttendanceId()+"'",
                new Object[]{},
                new BeanPropertyRowMapper<>(Attendance.class));
       if (result.size()>=1)
           return result.get(0);
        else return null;
    }
}
